﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZX.HIS.IDAL;
using ZX.HIS.DBUtility;
using System.Collections.Generic;
using ZX.HIS.Common;//Please add references
namespace ZX.HIS.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:DocAdvice
    /// </summary>
    public partial class DocAdvice : IDocAdvice
    {
        public DocAdvice()
        { }


        #region 自写方法
        /// <summary>
        /// 添加常用医嘱，返回自增ID
        /// </summary>
        /// <param name="DAModel"></param>
        /// <returns></returns>
        public int DocAdviceAdd(ZX.HIS.Model.DocAdvice DAModel)
        {
            string ComText = "Proc_DocAdviceAdd";
            SqlParameter[] parameters = {
                     new SqlParameter("@HospitalId", SqlDbType.Int,4){Value=DAModel.HospitalId},
                     new SqlParameter("@DocAdviceName", SqlDbType.VarChar,100){Value=DAModel.DocAdviceName},
                     new SqlParameter("@DocAdviceContent", SqlDbType.VarChar,1000){Value=DAModel.DocAdviceContent},
                     new SqlParameter("@DocAdviceIsAll", SqlDbType.Int,4){Value=DAModel.DocAdviceIsAll},
                     new SqlParameter("@UserId", SqlDbType.Int,4){Value=DAModel.UserId}
                                        };
            object obj = SqlDBhelper.RunProcedureScalar(ComText, parameters);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="DAModel"></param>
        /// <returns></returns>
        public bool DocAdviceUpt(ZX.HIS.Model.DocAdvice DAModel)
        {
            string ComText = "Proc_DocAdviceUpt";
            SqlParameter[] parameters = {
                     new SqlParameter("@DocAdviceName", SqlDbType.VarChar,100){Value=DAModel.DocAdviceName},
                     new SqlParameter("@DocAdviceContent", SqlDbType.VarChar,1000){Value=DAModel.DocAdviceContent},
                     new SqlParameter("@DocAdviceIsAll", SqlDbType.Int,8){Value=DAModel.DocAdviceIsAll},
                     new SqlParameter("@DocAdviceId", SqlDbType.Int,8){Value=DAModel.DocAdviceId}
                                        };
            return SqlDBhelper.RunProcedureNonQuery(ComText, parameters);
        }
        /// <summary>
        /// 分页显示医嘱表
        /// </summary>
        /// <param name="PageIndex">页数</param>
        /// <param name="PageSize">条数</param>
        /// <param name="DAModel">实体条件</param>
        /// <returns></returns>
        public PageClass<ZX.HIS.Model.DocAdvice> DocAdvicePageList(int PageIndex,int PageSize,ZX.HIS.Model.DocAdvice DAModel)
        {
             string ComText = "Proc_DocAdvicePageList";
             SqlParameter[] parameters = {
                     new SqlParameter("@UserId", SqlDbType.Int,8){Value=DAModel.UserId},
                     new SqlParameter("@PageIndex", SqlDbType.Int,8){Value=PageIndex},
                     new SqlParameter("@PageSize", SqlDbType.Int,8){Value=PageSize},
                     new SqlParameter("@HospitalId", SqlDbType.Int,8){Value=DAModel.HospitalId}
                                        };
             int PageNum = 0;
             List<ZX.HIS.Model.DocAdvice> PList = new List<ZX.HIS.Model.DocAdvice>();
             DataTable dt = SqlDBhelper.RunProcedure(ComText, parameters, PageSize, PageIndex, out PageNum);
             for (int i = 0; i < dt.Rows.Count; i++)
             {
                 DAModel = DataRowToModel(dt.Rows[i]);
                 PList.Add(DAModel);
             }
             PageClass<ZX.HIS.Model.DocAdvice> Page = new PageClass<Model.DocAdvice>();
             Page.PageIndex = PageIndex;
             Page.PageSize = PageSize;
             Page.PageNum = PageNum;
             Page.PageCount = PageNum / PageSize;
             int PageCount = PageNum % PageSize;
             Page.PageList = PList;
             if (PageCount > 0)
             {
                 Page.PageCount = Page.PageCount + 1;
             }
             return Page;
            
        }
        /// <summary>
        ///查询常用医嘱，添加诊断表用
        /// </summary>
        /// <param name="DAModel"></param>
        /// <returns></returns>
        public List<ZX.HIS.Model.DocAdvice> DocAdviceList(ZX.HIS.Model.DocAdvice DAModel)
        {
            string ComText = "Proc_DocAdviceList";
            SqlParameter[] parameters = {
                     new SqlParameter("@UserId", SqlDbType.Int,8){Value=DAModel.UserId},
                     new SqlParameter("@DocAdviceIsAll", SqlDbType.Int,8){Value=DAModel.DocAdviceIsAll},
                     new SqlParameter("@HospitalId", SqlDbType.Int,8){Value=DAModel.HospitalId}
                                        };
            List<ZX.HIS.Model.DocAdvice> DAList = new List<Model.DocAdvice>();
            DataSet ds = SqlDBhelper.RunProcedure(ComText, parameters, "db");
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DAModel = DataRowToModel(ds.Tables[0].Rows[0]);
                    DAList.Add(DAModel);
                }
                return DAList;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个实体，常用医嘱
        /// </summary>
        /// <param name="DocAdviceId"></param>
        /// <returns></returns>
        public ZX.HIS.Model.DocAdvice GetDocAdviceModel(int DocAdviceId)
        {
            string ComText = "Proc_DocAdviceModel";
            SqlParameter[] parameters = {
                    new SqlParameter("@DocAdviceId", SqlDbType.Int,8){Value=DocAdviceId}
            };

            DataSet ds = SqlDBhelper.RunProcedure(ComText, parameters, "db");
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZX.HIS.Model.DocAdvice DataRowToModel(DataRow row)
        {
            ZX.HIS.Model.DocAdvice model = new ZX.HIS.Model.DocAdvice();
            if (row != null)
            {
                if (row["DocAdviceId"] != null && row["DocAdviceId"].ToString() != "")
                {
                    model.DocAdviceId = int.Parse(row["DocAdviceId"].ToString());
                }
                if (row["HospitalId"] != null && row["HospitalId"].ToString() != "")
                {
                    model.HospitalId = int.Parse(row["HospitalId"].ToString());
                }
                if (row["DocAdviceName"] != null)
                {
                    model.DocAdviceName = row["DocAdviceName"].ToString();
                }
                if (row["DocAdviceContent"] != null)
                {
                    model.DocAdviceContent = row["DocAdviceContent"].ToString();
                }
                if (row["DocAdviceIsAll"] != null && row["DocAdviceIsAll"].ToString() != "")
                {
                    model.DocAdviceIsAll = int.Parse(row["DocAdviceIsAll"].ToString());
                }
                if (row["UserId"] != null && row["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(row["UserId"].ToString());
                }
            }
            return model;
        }
        #endregion

        #region  BasicMethod

        ///// <summary>
        ///// 得到最大ID
        ///// </summary>
        //public int GetMaxId()
        //{
        //return DbHelperSQL.GetMaxID("DocAdviceId", "tbDocAdvice"); 
        //}

        ///// <summary>
        ///// 是否存在该记录
        ///// </summary>
        //public bool Exists(int DocAdviceId)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) from tbDocAdvice");
        //    strSql.Append(" where DocAdviceId=@DocAdviceId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@DocAdviceId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = DocAdviceId;

        //    return DbHelperSQL.Exists(strSql.ToString(),parameters);
        //}


        ///// <summary>
        ///// 增加一条数据
        ///// </summary>
        //public int Add(ZX.HIS.Model.DocAdvice model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("insert into tbDocAdvice(");
        //    strSql.Append("HospitalId,DocAdviceName,DocAdviceContent,DocAdviceIsAll,UserId)");
        //    strSql.Append(" values (");
        //    strSql.Append("@HospitalId,@DocAdviceName,@DocAdviceContent,@DocAdviceIsAll,@UserId)");
        //    strSql.Append(";select @@IDENTITY");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@DocAdviceName", SqlDbType.VarChar,100),
        //            new SqlParameter("@DocAdviceContent", SqlDbType.VarChar,1000),
        //            new SqlParameter("@DocAdviceIsAll", SqlDbType.Int,4),
        //            new SqlParameter("@UserId", SqlDbType.Int,4)};
        //    parameters[0].Value = model.HospitalId;
        //    parameters[1].Value = model.DocAdviceName;
        //    parameters[2].Value = model.DocAdviceContent;
        //    parameters[3].Value = model.DocAdviceIsAll;
        //    parameters[4].Value = model.UserId;

        //    object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 更新一条数据
        ///// </summary>
        //public bool Update(ZX.HIS.Model.DocAdvice model)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("update tbDocAdvice set ");
        //    strSql.Append("HospitalId=@HospitalId,");
        //    strSql.Append("DocAdviceName=@DocAdviceName,");
        //    strSql.Append("DocAdviceContent=@DocAdviceContent,");
        //    strSql.Append("DocAdviceIsAll=@DocAdviceIsAll,");
        //    strSql.Append("UserId=@UserId");
        //    strSql.Append(" where DocAdviceId=@DocAdviceId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
        //            new SqlParameter("@DocAdviceName", SqlDbType.VarChar,100),
        //            new SqlParameter("@DocAdviceContent", SqlDbType.VarChar,1000),
        //            new SqlParameter("@DocAdviceIsAll", SqlDbType.Int,4),
        //            new SqlParameter("@UserId", SqlDbType.Int,4),
        //            new SqlParameter("@DocAdviceId", SqlDbType.Int,4)};
        //    parameters[0].Value = model.HospitalId;
        //    parameters[1].Value = model.DocAdviceName;
        //    parameters[2].Value = model.DocAdviceContent;
        //    parameters[3].Value = model.DocAdviceIsAll;
        //    parameters[4].Value = model.UserId;
        //    parameters[5].Value = model.DocAdviceId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        ///// <summary>
        ///// 删除一条数据
        ///// </summary>
        //public bool Delete(int DocAdviceId)
        //{

        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbDocAdvice ");
        //    strSql.Append(" where DocAdviceId=@DocAdviceId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@DocAdviceId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = DocAdviceId;

        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}
        ///// <summary>
        ///// 批量删除数据
        ///// </summary>
        //public bool DeleteList(string DocAdviceIdlist )
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("delete from tbDocAdvice ");
        //    strSql.Append(" where DocAdviceId in ("+DocAdviceIdlist + ")  ");
        //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
        //    if (rows > 0)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}


        ///// <summary>
        ///// 得到一个对象实体
        ///// </summary>
        //public ZX.HIS.Model.DocAdvice GetModel(int DocAdviceId)
        //{

        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select  top 1 DocAdviceId,HospitalId,DocAdviceName,DocAdviceContent,DocAdviceIsAll,UserId from tbDocAdvice ");
        //    strSql.Append(" where DocAdviceId=@DocAdviceId");
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@DocAdviceId", SqlDbType.Int,4)
        //    };
        //    parameters[0].Value = DocAdviceId;

        //    ZX.HIS.Model.DocAdvice model=new ZX.HIS.Model.DocAdvice();
        //    DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
        //    if(ds.Tables[0].Rows.Count>0)
        //    {
        //        return DataRowToModel(ds.Tables[0].Rows[0]);
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}


        ///// <summary>
        ///// 得到一个对象实体
        ///// </summary>
        //public ZX.HIS.Model.DocAdvice DataRowToModel(DataRow row)
        //{
        //    ZX.HIS.Model.DocAdvice model=new ZX.HIS.Model.DocAdvice();
        //    if (row != null)
        //    {
        //        if(row["DocAdviceId"]!=null && row["DocAdviceId"].ToString()!="")
        //        {
        //            model.DocAdviceId=int.Parse(row["DocAdviceId"].ToString());
        //        }
        //        if(row["HospitalId"]!=null && row["HospitalId"].ToString()!="")
        //        {
        //            model.HospitalId=int.Parse(row["HospitalId"].ToString());
        //        }
        //        if(row["DocAdviceName"]!=null)
        //        {
        //            model.DocAdviceName=row["DocAdviceName"].ToString();
        //        }
        //        if(row["DocAdviceContent"]!=null)
        //        {
        //            model.DocAdviceContent=row["DocAdviceContent"].ToString();
        //        }
        //        if(row["DocAdviceIsAll"]!=null && row["DocAdviceIsAll"].ToString()!="")
        //        {
        //            model.DocAdviceIsAll=int.Parse(row["DocAdviceIsAll"].ToString());
        //        }
        //        if(row["UserId"]!=null && row["UserId"].ToString()!="")
        //        {
        //            model.UserId=int.Parse(row["UserId"].ToString());
        //        }
        //    }
        //    return model;
        //}

        ///// <summary>
        ///// 获得数据列表
        ///// </summary>
        //public DataSet GetList(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select DocAdviceId,HospitalId,DocAdviceName,DocAdviceContent,DocAdviceIsAll,UserId ");
        //    strSql.Append(" FROM tbDocAdvice ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获得前几行数据
        ///// </summary>
        //public DataSet GetList(int Top,string strWhere,string filedOrder)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select ");
        //    if(Top>0)
        //    {
        //        strSql.Append(" top "+Top.ToString());
        //    }
        //    strSql.Append(" DocAdviceId,HospitalId,DocAdviceName,DocAdviceContent,DocAdviceIsAll,UserId ");
        //    strSql.Append(" FROM tbDocAdvice ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    strSql.Append(" order by " + filedOrder);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///// <summary>
        ///// 获取记录总数
        ///// </summary>
        //public int GetRecordCount(string strWhere)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("select count(1) FROM tbDocAdvice ");
        //    if(strWhere.Trim()!="")
        //    {
        //        strSql.Append(" where "+strWhere);
        //    }
        //    object obj = DbHelperSQL.GetSingle(strSql.ToString());
        //    if (obj == null)
        //    {
        //        return 0;
        //    }
        //    else
        //    {
        //        return Convert.ToInt32(obj);
        //    }
        //}
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        //{
        //    StringBuilder strSql=new StringBuilder();
        //    strSql.Append("SELECT * FROM ( ");
        //    strSql.Append(" SELECT ROW_NUMBER() OVER (");
        //    if (!string.IsNullOrEmpty(orderby.Trim()))
        //    {
        //        strSql.Append("order by T." + orderby );
        //    }
        //    else
        //    {
        //        strSql.Append("order by T.DocAdviceId desc");
        //    }
        //    strSql.Append(")AS Row, T.*  from tbDocAdvice T ");
        //    if (!string.IsNullOrEmpty(strWhere.Trim()))
        //    {
        //        strSql.Append(" WHERE " + strWhere);
        //    }
        //    strSql.Append(" ) TT");
        //    strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
        //    return DbHelperSQL.Query(strSql.ToString());
        //}

        ///*
        ///// <summary>
        ///// 分页获取数据列表
        ///// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //    SqlParameter[] parameters = {
        //            new SqlParameter("@tblName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@fldName", SqlDbType.VarChar, 255),
        //            new SqlParameter("@PageSize", SqlDbType.Int),
        //            new SqlParameter("@PageIndex", SqlDbType.Int),
        //            new SqlParameter("@IsReCount", SqlDbType.Bit),
        //            new SqlParameter("@OrderType", SqlDbType.Bit),
        //            new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
        //            };
        //    parameters[0].Value = "tbDocAdvice";
        //    parameters[1].Value = "DocAdviceId";
        //    parameters[2].Value = PageSize;
        //    parameters[3].Value = PageIndex;
        //    parameters[4].Value = 0;
        //    parameters[5].Value = 0;
        //    parameters[6].Value = strWhere;	
        //    return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        //}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

